package model

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

type LocalTime time.Time

func (t *LocalTime) MarshalJSON() ([]byte, error) {
	// LocalTime 转换成 time.Time 类型
	tTime := time.Time(*t)
	return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02"))), nil
}

type CustInfo struct {
	Id        int64     `gorm:"primary;type:int(11)" json:"id"`
	CustId    string    `gorm:"type:varchar(20);not null" json:"custId"`
	Mobile    string    `gorm:"type:varchar(20);not null" json:"mobile"`
	RealName  string    `gorm:"type:varchar(20)" json:"realName"`
	Province  string    `gorm:"type:varchar(20)" json:"province"`
	City      string    `gorm:"type:varchar(20)" json:"city"`
	Address   string    `gorm:"type:varchar(200)" json:"address"`
	LoginIp   string    `gorm:"type:varchar(50)" json:"loginIp"`
	LoginTime LocalTime `gorm:"type:datetime" json:"loginTime"`
	Enabled   uint8     `gorm:"type:tinyint(4)" json:"enabled"`
	VipLevel  uint8     `gorm:"type:tinyint(4)" json:"vipLevel"`
	PushRatio *float64  `gorm:"type:double" json:"pushRatio"`
	RiskRatio *float64  `gorm:"type:double" json:"riskRatio"`
}

// get info from custId or mobile
func GetInfo(tx *gorm.DB, custInfo *CustInfo) error {
	result := tx.Where(custInfo).First(custInfo)
	if result.Error != nil {
		return result.Error
	}
	return nil
}

// 按条件查询所有符合条件的客户信息
func GetAllCustInfoByCondition(tx *gorm.DB, custIds *[]string, query string, conditions []interface{}) error {

	// 使用 conditions... 来解包 slice，传递给 Where 方法
	result := tx.Debug().Model(&CustInfo{}).Select("cust_id").Where(query, conditions...).Find(custIds)
	if result.Error != nil {
		return result.Error
	}
	return nil
}
